package logic

import (
	"context"
	"vip-shop-hub/models/mysql"

	"vip-shop-hub/viprpc/internal/svc"
	"vip-shop-hub/viprpc/viprpc"

	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type ProductAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductAddLogic) ProductAdd(in *viprpc.ProductAddRequest) (*viprpc.ProductAddResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	if err := user.FIndUserBY(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("当前用户不存在")
	}
	product := mysql.Product{
		Model:        gorm.Model{},
		ProductName:  in.ProductName,
		ProductPrice: float64(in.ProductPrice),
		ProductImage: in.ProductImage,
		ProductStock: in.ProductStock,
		UserId:       int64(user.ID),
	}

	if err := product.ProuAdd(mysql.DB); err != nil {
		return nil, errors.New("商品添加失败")
	}
	return &viprpc.ProductAddResponse{
		ProductId: int64(product.ID),
	}, nil
}
